package com.jams.goodox.activity.serve.express;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.lvman.configs.instrumentation.InstrumentedDraweeView;
import com.jams.goodox.R;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.entity.ExpressBean;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.ServiceService;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.ToastUtil;
import com.jams.library.commonAdapter.ListCommonAdapter;
import com.jams.library.commonAdapter.ListCommonViewHolder;
import com.jams.library.fresco.FrescoUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import retrofit2.Call;

/**
 * Created by jams_zhen on 2016/10/14.
 */

public class ExpressCompanyList extends BaseActivity implements AdapterView.OnItemClickListener {
    @Bind(R.id.lst)
    ListView lst;
    @Bind(R.id.no_data_layout)
    LinearLayout no_data_layout;
    @Bind(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;

    private ListCommonAdapter listCommonAdapter;
    private List<ExpressBean.DataBean> dataBeanList;

    private ExpressBean.DataBean expressDateBean;

    private int oldPosi = -1;//历史选中项
    private boolean isRefresh = false;
    private String subId;

    @Override
    protected int getLayoutId() {
        return R.layout.express_company_list;
    }

    @Override
    protected void init() {
        StyleUtil.titleBackKey(this, "选择快递公司");
        expressDateBean = (ExpressBean.DataBean) getIntent().getSerializableExtra("expressDateBean");
        subId=getIntent().getStringExtra("subId");

        dataBeanList = new ArrayList<>();
        listCommonAdapter = new ListCommonAdapter<ExpressBean.DataBean>(this, dataBeanList, R.layout.item_express) {
            @Override
            public void convert(ListCommonViewHolder helper, ExpressBean.DataBean item, int position) {
                InstrumentedDraweeView img_icon = helper.getView(R.id.img_icon);
                TextView tx_name = helper.getView(R.id.tx_name);
                ImageView img_choose = helper.getView(R.id.img_choose);
                FrescoUtil.loadImg(img_icon, item.getExpIcon());
                tx_name.setText(item.getExpFirm() + "");
                if (item.isChoose()) {
                    img_choose.setVisibility(View.VISIBLE);
                } else {
                    img_choose.setVisibility(View.GONE);
                }
            }
        };
        lst.setAdapter(listCommonAdapter);
        lst.setOnItemClickListener(this);
        swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                isRefresh = true;
                getDate();
            }
        });
        getDate();
    }

    private void getDate() {
        if (!isRefresh) {
//            showLoading();
        }
        enqueue(createCallService(ServiceService.class).queryExpressCompany(subId+""), new RetrofitCallbackListener<ExpressBean>() {
            @Override
            public void onSuccess(Call<ExpressBean> call, ExpressBean result) {
//                misLoading();
                swipeLayout.setRefreshing(false);
                dataBeanList.clear();
                if(result.getData()==null){
                    no_data_layout.setVisibility(View.VISIBLE);
                    return;
                }
                if ( result.getData().size() > 0) {
                    dataBeanList.addAll(result.getData());
                    if (expressDateBean != null) {
                        setSelection();
                    }
                    listCommonAdapter.notifyDataSetChanged();
                }else {
                    no_data_layout.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onError(String msg) {
//                misLoading();
                swipeLayout.setRefreshing(false);
                ToastUtil.show(msg);
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (oldPosi >= 0) {
            dataBeanList.get(oldPosi).setChoose(false);
        }
        dataBeanList.get(position).setChoose(true);
        oldPosi = position;
        listCommonAdapter.notifyDataSetChanged();
        Intent in = new Intent(this, InputExpressActivity.class);
        in.putExtra("expressDateBean", dataBeanList.get(position));
        setResult(RESULT_OK, in);
        finish();
    }

    /**
     * 设置默认选中项
     */
    private void setSelection() {
        for (int i = 0, len = dataBeanList.size(); i < len; i++) {
            if (dataBeanList.get(i).getExpCode().equals(expressDateBean.getExpCode())) {
                oldPosi = i;
                dataBeanList.get(i).setChoose(true);
                return;
            }
        }
    }
}
